package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.goods.GoodsBO;
import com.ymk.bo.goods.GoodsCartBO;
import com.ymk.bo.goods.GoodsSkuBO;
import com.ymk.core.enums.goods.GoodsTypeEnum;
import com.ymk.service.IGoodsCartService;
import com.ymk.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

//购物车
@Service
public class GoodsCartPostHzx extends ACmd {

    @Override
    public CmdSign executeStore(HttpServletRequest request,CmdSign sign)throws Exception  {
        CmdSign respSign = new CmdSign(sign);  //主要用于返回
      GoodsCartBO goodsCartBO=super.getData(sign, GoodsCartBO.class);
      //先查询改商品是否存在该用户的购物车中，如果有的话就在此购物车中添加数量即可
        //因为要确定某个用户的购物车，所以要知道是那个用户在那个商店然后那个商店，所以需要用到三个id
        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("goodsId",goodsCartBO.getGoodsId())
                .addParam("goodSku",goodsCartBO.getSkuId())
               // .addParam("userId",goodsCartBO.getUserId()) 因为用户已经登录了，所以要根据登录情况去判断用户，而不能采用参数了
                .addParam("userId",sign.getUserId());
        //依据上述条件查询看是否有存在的购物车
     GoodsCartBO  dbgoodsCartBO   =    this.gsc.get(paramsBuilder.build());
     if(Objects.isNull(dbgoodsCartBO)){
         //不是空说明已经存在此类购物车了，我们需要在购物车中加入此次购买的商品
        GoodsCartBO goodsCartBO1=new GoodsCartBO();
        goodsCartBO1.setGoodsId(dbgoodsCartBO.getGoodsId());
        goodsCartBO1.setGoodsCount(dbgoodsCartBO.getGoodsCount()+goodsCartBO.getGoodsCount());
          this.gsc.update(goodsCartBO1);


        return respSign;
     }
     //以下就是购物车不存在要创建一个购物车的过程
              paramsBuilder.clearParam()         //先清空下map，因为下面还要用到这个map
                      .addParam("id",goodsCartBO.getGoodsId())
                      .addParam("loadSku",true);//这个是打开多表联合，以此联合商品表和商品sku表，以查询更多信息
        GoodsBO  goodsBO= this.gs.get(paramsBuilder.build());

        GoodsCartBO goodsCartBO1 = new GoodsCartBO();  //创建一个购物车
        goodsCartBO1.setMchId(sign.getMchId());//设置商户
        goodsCartBO1.setUserId(sign.getUserId());//设置用户
        goodsCartBO1.setGoodsId(goodsBO.getId());//设置商品

        //接下来就是商品的sku了，因为用了多表联合查询，有一些信息在GoodsBO的goodsSkuBOList中写着
        Map<Integer, GoodsSkuBO> goodsSkuBOMap = goodsBO.getGoodsSkuBOList().stream().collect(Collectors.toMap(GoodsSku::getId, x -> x));
        GoodsSkuBO goodsSkuBO = goodsSkuBOMap.get(goodsCartBO.getSkuId());
        goodsCartBO1.setSkuId(goodsSkuBO.getId());

        if (GoodsTypeEnum.DISABLED.getType().equals(goodsBO.getType())){
            goodsCartBO1.setSkuName(goodsBO.getTitle());
        }else{
            goodsCartBO1.setSkuName(goodsBO.getTitle() + goodsSkuBO.getPropName());
        }

        goodsCartBO1.setGoodsCount(goodsCartBO.getGoodsCount());
        goodsCartBO1.setRemark(goodsCartBO.getRemark());
        super.setCreatedData(sign,goodsCartBO1);

        this.gsc.post(goodsCartBO1);

        return respSign;

    }

//验证
    @Override
    public CmdSign verify(HttpServletRequest request,CmdSign sign)throws Exception {
        GoodsCartBO goodsCartBO= super.getData(sign,GoodsCartBO.class);
        if (Objects.isNull(goodsCartBO.getGoodsId())){
            throw  new MallException("商品id为空");
        }
        return null;
  }






   @Override
    public String getCmdCode() {return  null;}//进入这个方法的命令的代码

    //购物车
    @Autowired
    private IGoodsCartService gsc;
    //商品
    @Autowired
    private IGoodsService gs;

}
